package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingCadetAttendance;
import com.ruoyi.system.domain.TrainingCadetAttendanceReport;
import com.ruoyi.system.mapper.TrainingCadetAttendanceMapper;
import com.ruoyi.system.service.ITrainingCadetAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 培训考勤Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingCadetAttendanceServiceImpl implements ITrainingCadetAttendanceService {
    @Autowired
    private TrainingCadetAttendanceMapper trainingCadetAttendanceMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询培训考勤
     *
     * @param attendanceId 培训考勤主键
     * @return 培训考勤
     */
    @Override
    public TrainingCadetAttendance selectTrainingCadetAttendanceByAttendanceId(Long attendanceId) {
        return trainingCadetAttendanceMapper.selectTrainingCadetAttendanceByAttendanceId(attendanceId);
    }

    /**
     * 查询培训考勤列表
     *
     * @param trainingCadetAttendance 培训考勤
     * @return 培训考勤
     */
    @Override
    public List<TrainingCadetAttendance> selectTrainingCadetAttendanceList(TrainingCadetAttendance trainingCadetAttendance) {
        return trainingCadetAttendanceMapper.selectTrainingCadetAttendanceList(trainingCadetAttendance);
    }

    /**
     * 查询培训考勤报表
     *
     * @param trainingCadetAttendance 培训考勤
     * @return 培训考勤
     */
    @Override
    public List<TrainingCadetAttendanceReport> selectTrainingCadetAttendanceReport(TrainingCadetAttendanceReport trainingCadetAttendance) {
        return trainingCadetAttendanceMapper.selectTrainingCadetAttendanceReport(trainingCadetAttendance);
    }

    /**
     * 查询培训考勤明细
     * @param trainingCadetAttendance
     * @return
     */
    @Override
    public List<TrainingCadetAttendance> getAttendanceList(TrainingCadetAttendanceReport trainingCadetAttendance) {
        return trainingCadetAttendanceMapper.getAttendanceList(trainingCadetAttendance);
    }

    /**
     * 新增培训考勤
     *
     * @param trainingCadetAttendance 培训考勤
     * @return 结果
     */
    @Override
    public int insertTrainingCadetAttendance(TrainingCadetAttendance trainingCadetAttendance) {
        trainingCadetAttendance.setCreateTime(DateUtils.getNowDate());
        if (trainingCadetAttendance.getMentorId() != null && trainingCadetAttendance.getMentorId().isEmpty()) {
            trainingCadetAttendance.setMentorId(null);
        }
        return trainingCadetAttendanceMapper.insertTrainingCadetAttendance(trainingCadetAttendance);
    }

    /**
     * 修改培训考勤
     *
     * @param trainingCadetAttendance 培训考勤
     * @return 结果
     */
    @Override
    public int updateTrainingCadetAttendance(TrainingCadetAttendance trainingCadetAttendance) {
        if (trainingCadetAttendance.getMentorId() != null && trainingCadetAttendance.getMentorId().isEmpty()) {
            trainingCadetAttendance.setMentorId(null);
        }
        return trainingCadetAttendanceMapper.updateTrainingCadetAttendance(trainingCadetAttendance);
    }

    /**
     * 批量删除培训考勤
     *
     * @param attendanceIds 需要删除的培训考勤主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetAttendanceByAttendanceIds(Long[] attendanceIds) {
        return trainingCadetAttendanceMapper.deleteTrainingCadetAttendanceByAttendanceIds(attendanceIds);
    }

    /**
     * 删除培训考勤信息
     *
     * @param attendanceId 培训考勤主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetAttendanceByAttendanceId(Long attendanceId) {
        return trainingCadetAttendanceMapper.deleteTrainingCadetAttendanceByAttendanceId(attendanceId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingCadetAttendance 实体
     * @param userId                  用户id
     * @param crateType               创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingCadetAttendance trainingCadetAttendance, Long userId, Integer crateType) {
        //主键
        if (trainingCadetAttendance.getAttendanceId() == null || "".equals(trainingCadetAttendance.getAttendanceId())) {
            trainingCadetAttendance.setAttendanceId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingCadetAttendance.getCreateBy() == null) {
                trainingCadetAttendance.setCreateBy(userId.toString());
            }
            if (trainingCadetAttendance.getCreateTime() == null) {
                trainingCadetAttendance.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingCadetAttendance.getModifyBy() == null) {
            trainingCadetAttendance.setModifyBy(userId);
        }
        //修改时间
        if (trainingCadetAttendance.getModifyTime() == null) {
            trainingCadetAttendance.setModifyTime(DateUtils.getNowDate());
        }
    }

}
